package gl.android.xmppandroidclient;

import gl.android.APP;
import gl.android.xmppandroidclient.db.MessagePersistencer;
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.RemoteException;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;

@TargetApi(Build.VERSION_CODES.KITKAT)
public class DemoActivity extends ActionBarActivity implements OnClickListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			Window window = getWindow();
			// Translucent status bar
			window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
			// Translucent navigation bar
			window.setFlags(
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
		}
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main);

		if (savedInstanceState == null) {
			getSupportFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}

	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_main, container,
					false);
			return rootView;
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button1:
			try {

//				mChat.sendMessage("yang@dboomsky.com", "MessageBody:"
//						+ ((EditText) findViewById(R.id.editText1)).getText());
				((APP)getApplication()).xmpp.sendMsg("253010890", "MessageBody:"
						+ ((EditText) findViewById(R.id.editText1)).getText());
			} catch (RemoteException e) {
				e.printStackTrace();
			}
			break;
		case R.id.button2:
			Intent intent = new Intent();
			intent.setClass(this, DemoActivity.this.getClass());
			startActivity(intent);

			break;
		case R.id.button3:
			MessagePersistencer mMessagePersistencer = new MessagePersistencer(
					getApplicationContext());
			mMessagePersistencer.select("sa");
			mMessagePersistencer.destory();
			break;

		default:
			break;
		}

	}

}
